/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sdc;

import java.util.regex.*;
import java.awt.Color;

/**
 *
 * @author Don
 */
public class Post {
    
    public long number = 0;
    public String time = "";
    public String user = "";
    public String postURL = "";
    public String message = "";
    public Color color;
    
    public Post(String text, Color color) {
        // Clean up text
        Pattern timeNumberLinkPattern = Pattern.compile("<div class=\"sd_postbit(?: sd_postbit_last)?\" id=\"[^\"]*\">[^<]*<div class=\"sd_postbit_head ui-corner-top\" >[^<]*<div class=\"sd_postbit_head_right\">[^<]*<\\!-- checks for warning system -->.*?<\\!-- end of warning system -->[^<]*<a href=\"([^\"]*)\"[^>]*><strong>([0-9]*)</strong></a>[^<]*</div>[^<]*<a[^>]*>[^<]*<img[^>]*></a>[^A-Za-z0-9, :-]*([A-Za-z0-9, :-]*)[^<]*</div>");
        Pattern userPattern = Pattern.compile("<div class=\"sd_postbit_user ui-corner-small\"[^>]*>[^<]*<a[^>]*>(?:<b>|<span[^>]*>)?([^<]*)(?:</b>|</span>)?[^<]*<img[^>]*>");
        Pattern messagePattern = Pattern.compile("<!-- google_ad_section_start -->(.*?)<!-- google_ad_section_end -->");
        Pattern quotePattern = Pattern.compile("<table[^>]*>[^<]*<tr>[^<]*<td[^>]*>(.*?)</td>[^<]*</tr>[^<]*</table>");
        Pattern quoteLinkPattern = Pattern.compile("\"([^\"]*?) ?link:[^ ]* ?(:)([^\"]*?)\""); // Just used for removing the quote links for now... this will change
        Pattern divPattern = Pattern.compile("<div[^>]*>|</div>");
        Pattern imgPattern = Pattern.compile("<img[^>]*?src=\"([^\"]*)\"[^>]*>");
        Pattern linkPattern = Pattern.compile("<a href=\"([^\"]*)\"[^>]*>[^<]*</a>");
        // Get the number
        Matcher matcher = timeNumberLinkPattern.matcher(text);
        while (matcher.find()) {
            this.postURL = "http://www.slickdeals.net/" + matcher.group(1).replace("&amp;", "&");
            this.number = Long.parseLong(matcher.group(2));
            this.time = matcher.group(3);
        }
        // Get the user
        matcher = userPattern.matcher(text);
        while (matcher.find()) {
            this.user = matcher.group(1);
        }
        // Get the message
        matcher = messagePattern.matcher(text);
        while (matcher.find()) {
            this.message = matcher.group(1);
        }
        // Replace the quotes
        matcher = quotePattern.matcher(this.message);
        this.message = matcher.replaceAll("\"$1\"\r\n\r\n");
        // Remove any stray divs
        matcher = divPattern.matcher(this.message);
        this.message = matcher.replaceAll("");
        // Replace images
        matcher = imgPattern.matcher(this.message);
        this.message = matcher.replaceAll("img:$1");
        // Replace links
        matcher = linkPattern.matcher(this.message);
        this.message = matcher.replaceAll("link:$1");
        // Remove the quote links
        matcher = quoteLinkPattern.matcher(this.message);
        this.message = matcher.replaceAll("\"$1$2 $3\"");
        // Replace breaklines
        this.message = this.message.replaceAll("<br />", "\r\n");
        // Smilies
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-nod.gif", " :nod: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-puke.gif", " :puke: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-lookaround.gif", " :look: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/tongue.gif", " :P ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-sleepyhead.gif", " :sleepy: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-bulb.gif", " :idea: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/biggrin.gif", " :D ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-woot.gif", " :woot: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-eek.gif", " :OMG: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/frown.gif", " :( ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-yum.gif", " :yum: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-hug.gif", " :hug: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-LMAO.gif", " :LMAO: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-LOL.gif", " :LOL: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-rolleyes.gif", " :rolleyes: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies2/roll.gif", " :rolleyes: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/cool.gif", " :cool: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-shakehead.gif", " :shakehead: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-bounce.gif", " :bounce: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/wink.gif", " ;) ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-wave.gif", " :wave: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-drool.gif", " :drool: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/redface.gif", " :O ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-worship.gif", " :worship: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-heartbeat.gif", " <3 ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/smile.gif", " :) ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/emot-questioned.gif", " :???: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/eek.gif", " :eek: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies/mad.gif", " :mad: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies2/bigeye.gif", " :bigeyes: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies2/facepalm2.gif", " :facepalm: ");
        this.message = this.message.replace("img:http://st.slickdealz.net/images/smilies2/dontknow.gif", " :shrug: ");
        // Set the color
        this.color = color;
    }
    
    public String getPostURL() {
        return this.postURL;
    }
}
